package com.shchuangzhi.smartguard.core;

/**
 * Created by j_hu on 5/13/17.
 */

public class Teacher implements Swiper {
    private int no;
    private int deptNo;
    private String deptName;
    private String name;
    private String imgURL;
    private Card bindCard;
    private boolean deleted;
    private DBHelper dbHelper;
    private InOutRecord lastInOutRecord;

    private static Teacher current;

    public Teacher() {}

    public Teacher(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    public int getNo() { return no; }

    public void setNo(int no) { this.no = no; }

    public int getDeptNo() { return deptNo; }

    public void setDeptNo(int deptNo) { this.deptNo = deptNo; }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public String getImgURL() { return imgURL; }

    public void setImgURL(String imgURL) { this.imgURL = imgURL; }

    public boolean isDeleted() { return deleted; }

    public void setDeleted(boolean deleted) { this.deleted = deleted; }

    public Card getBindCard() {
        return bindCard;
    }

    public void setBindCard(Card card) {
        bindCard = card;
    }

    public InOutRecord getLastInOutRecord() {
        return lastInOutRecord;
    }

    public void setLastInOutRecord(InOutRecord record) {
        lastInOutRecord = record;
    }

    public CheckInOutResult swipe(int state) {
        return swipe(state, false);
    }

    public CheckInOutResult swipe(int state, boolean isManual) {
        int cardSeq = bindCard == null ? 1 : bindCard.getSeq();
        String stateName = TeacherState.getStateName(dbHelper, state);
        InOutRecord rec = new InOutRecord(
                0, Machine.getInstance().getId(), no, cardSeq, state, isManual, name, stateName, deptName, Clock.nowStr());
        CheckInOutResult result = checkState(rec);
        this.setLastInOutRecord(rec);
        Teacher.setCurrent(this);
        if (result.isOk()) {
            if (dbHelper.addInOut(rec)) {
                NotifyPrinter.getInstance().notify(
                        NotifyMessage.wrap(rec, Consts.NOTIFY_CHECK_IN_OUT_LIST, Consts.NOTIFY_CHECK_IN_OUT_MAIN));
            } else {
                result = CheckInOutResult.fail(Consts.SPEECH_CHECK_IN_DUPLICATED);
            }
        }

        if (!result.isOk()) {
            NotifyPrinter.getInstance().notify(
                    NotifyMessage.info(String.format("%s:%s:%s",
                            Consts.SWIPE_FAIL,
                            getBindCard().getNo(),
                            result.getVoice()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
        }
        return result;
    }

    private CheckInOutResult checkState(InOutRecord record) {
        CheckInOutResult result = CheckInOutResult.ok();
        if (record.getState() != Consts.STATE_TEACHER_IN && record.getState() != Consts.STATE_TEACHER_OUT) {
            return CheckInOutResult.fail(Consts.SPEECH_STATE_LOCKED);
        }

        if (current != null && current.getNo() == record.getNo()) {
            if (current.getLastInOutRecord().getState() == record.getState()) {
                if (current.getLastInOutRecord().isStateDup(record)) {
                    result = CheckInOutResult.fail(Consts.SPEECH_CHECK_IN_DUPLICATED);
                    return result;
                }
            }
        }
        return result;
    }

    public static void setCurrent(Teacher teacher) {
        current = teacher;
    }

    public static Teacher getCurrent() {
        return current;
    }
}
